﻿/*
** nopCommerce custom js functions
*/


function getE(name) {
    //Obsolete since nopCommerce 2.60. But still here for backwards compatibility (in case of some plugin developers used it in their plugins or customized solutions)
    if (document.getElementById)
        var elem = document.getElementById(name);
    else if (document.all)
        var elem = document.all[name];
    else if (document.layers)
        var elem = document.layers[name];
    return elem;
}

function OpenWindow(query, w, h, scroll) {
    var l = (screen.width - w) / 2;
    var t = (screen.height - h) / 2;

    winprops = 'resizable=0, height=' + h + ',width=' + w + ',top=' + t + ',left=' + l + 'w';
    if (scroll) winprops += ',scrollbars=1';
    var f = window.open(query, "_blank", winprops);
}

function setLocation(url) {
    window.location.href = url;
}

function displayAjaxLoading(display) {
    if (display) {
        $('.ajax-loading-block-window').show();
    }
    else {
        $('.ajax-loading-block-window').hide('slow');
    }
}
var notificationNumber = 0;
function displayPopupNotification(message, messagetype) {
    if (typeof message != "undefined" && typeof messagetype != undefined) {
        //types: success, error
        var typeclass;
        notificationNumber += 1;
        if (messagetype == 'success') {
            //success
            typeclass = 'dialog-notifications-success';
        }
        else if (messagetype == 'error') {
            //error
            typeclass = 'dialog-notifications-error';
        }
        else {
            //other
            typeclass = 'dialog-notifications-success';
        }

        //we do not encode displayed message
        var htmlcode = '<div class="notification notification-'+notificationNumber+' '+typeclass+' fadeout">';
        if ((typeof message) == 'string') {
            htmlcode += '<p>' + message + '</p>';
        } else {
            for (var i = 0; i < message.length; i++) {
                htmlcode += '<p>' + message[i] + '</p>';
            }
        }
        htmlcode += "<i class='fa fa-times'></i></div>";
        $(".notification").fadeOut(200, function () {
            $(this).remove();
        });
        $("body").append(htmlcode);
        $(".notification-" + notificationNumber).fadeIn(200, function () {
            var timeout = setTimeout(function () {
                $(".fadeout.notification-" + notificationNumber).fadeOut(200, function () {
                    $(".notification-" + notificationNumber).remove();
                });
            }, 2500);
        });
    }
}
$(".notification").live("mouseenter", function () {
    $(this).removeClass("fadeout");
});
$(".notification .fa-times").live("click", function () {
    $(this).closest(".notification").fadeOut(200, function () {
        $(this).remove();
    });
});

var barNotificationTimeout;
function displayBarNotification(message, messagetype, timeout) {
    clearTimeout(barNotificationTimeout);

    //types: success, error
    var cssclass = 'success';
    if (messagetype == 'success') {
        cssclass = 'success';
    }
    else if (messagetype == 'error') {
        cssclass = 'error';
    }
    //remove previous CSS classes and notifications
    $('#bar-notification')
        .removeClass('success')
        .removeClass('error');
    $('#bar-notification .content').remove();

    //we do not encode displayed message

    //add new notifications
    var htmlcode = '';
    if ((typeof message) == 'string') {
        htmlcode = '<p class="content">' + message + '</p>';
    } else {
        for (var i = 0; i < message.length; i++) {
            htmlcode = htmlcode + '<p class="content">' + message[i] + '</p>';
        }
    }
    $('#bar-notification').append(htmlcode)
        .addClass(cssclass)
        .fadeIn('slow')
        .mouseenter(function ()
            {
                clearTimeout(barNotificationTimeout);
            });

    $('#bar-notification .close').unbind('click').click(function () {
        $('#bar-notification').fadeOut('slow');
    });

    //timeout (if set)
    if (timeout > 0) {
        barNotificationTimeout = setTimeout(function () {
            $('#bar-notification').fadeOut('slow');
        }, timeout);
    }
}

function htmlEncode(value) {
    return $('<div/>').text(value).html();
}

function htmlDecode(value) {
    return $('<div/>').html(value).text();
}

// Define custom ajax-notification functions

function showNotification() {
    $('#ajax-notification').empty().fadeIn().append('<div class="ajax-loading" />');
}

function callbackNotification(data, title, redirect) {
    $('#ajax-notification').empty().append('<div class="ajax-loading-box window" />');
    if (title != null) {
        var titleBlock = '<div class="ajax-notification-title window-title grad-lightgrey grad"><div class="window-title-bar grad-titleshine grad"><h2>' + title + '</h2></div></div>';
    } else {
        var titleBlock = '';
    }
    if (data != null) {
        var contentBlock = '<div class="ajax-notification-content window-content space">' + data + '</div>';
    } else {
        var contentBlock = '';
    }
    if (redirect != null) {
        if (redirect != window.location.href.replace(location.protocol + '//' + location.host, '')) {
            redirect = '';
        }
    } else {
        redirect = '';
    }
    $('.ajax-loading-box').html(titleBlock + contentBlock + '<div class="ajax-notification-confirm" data-redirect="' + redirect + '">OK</div>').fadeIn();
}

// HTML5 placeholder support check
$.support.placeholder = (function () {
    var i = document.createElement('input');
    return 'placeholder' in i;
})();

// Get url parameter modification
function addParameter(url, parameterName, parameterValue, atStart/*Add param before others*/) {
    replaceDuplicates = true;
    if (url.indexOf('#') > 0) {
        var cl = url.indexOf('#');
        urlhash = url.substring(url.indexOf('#'), url.length);
    } else {
        urlhash = '';
        cl = url.length;
    }
    sourceUrl = url.substring(0, cl);

    var urlParts = sourceUrl.split("?");
    var newQueryString = "";

    if (urlParts.length > 1) {
        var parameters = urlParts[1].split("&");
        for (var i = 0; (i < parameters.length) ; i++) {
            var parameterParts = parameters[i].split("=");
            if (!(replaceDuplicates && parameterParts[0] == parameterName)) {
                if (newQueryString == "")
                    newQueryString = "?";
                else
                    newQueryString += "&";
                newQueryString += parameterParts[0] + "=" + (parameterParts[1] ? parameterParts[1] : '');
            }
        }
    }
    if (newQueryString == "")
        newQueryString = "?";

    if (atStart) {
        newQueryString = '?' + parameterName + "=" + parameterValue + (newQueryString.length > 1 ? '&' + newQueryString.substring(1) : '');
    } else {
        if (newQueryString !== "" && newQueryString != '?')
            newQueryString += "&";
        newQueryString += parameterName + "=" + (parameterValue ? parameterValue : '');
    }
    return urlParts[0] + newQueryString + urlhash;
};